/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-02 11:48:02
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-07 16:28:22
 */
import { ApiResponse, User, UserProfile } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'

export function getUserAction(): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    // console.log(res, 2)
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data
    })
  }
}

export const getUserProfileAction = (): RootThunkAction => {
  return async dispatch => {
    const res = await request.get<ApiResponse<UserProfile>>('/user/profile')
    dispatch({ type: 'profile/getUserProfile', payload: res.data.data })
  }
}

export function updateUserProfile(type: string, value: string): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/profile', {
      [type]: value
    })
    // 重新发请求拿数据
    dispatch(getUserProfileAction())
  }
}

export const updateUserPhotoAction = (data: FormData): RootThunkAction => {
  return async (dispatch) => {
    await request.patch('/user/photo', data)

    // 重新渲染
    dispatch(getUserProfileAction())
  }
}
